package pro.jiug.demo.config;

import org.quartz.JobDataMap;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import pro.jiug.demo.job.MyJob;
import pro.jiug.demo.service.impl.HelloServiceImpl;

/**
 * 定时任务配置
 */
@Configuration
public class ScheduleConfig {

    /**
     * 配置JobDetailFactory，此种方式无法传参，且JOB任务上要添加@Component注解，将定时任务注入到Spring容器中
     * @return
     */
    /*@Bean
    MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean(){
        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean=new MethodInvokingJobDetailFactoryBean();
        methodInvokingJobDetailFactoryBean.setTargetBeanName("testQuartzJob");
        methodInvokingJobDetailFactoryBean.setTargetMethod("job");
        return methodInvokingJobDetailFactoryBean;
    }*/
    /**
     * 配置JobDetailFactory，此种方式可以传参
     * @return
     */
    @Bean
    JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean bean=new JobDetailFactoryBean();
        bean.setJobClass(MyJob.class);
        //设置参数
        JobDataMap map=new JobDataMap();
        map.put("helloService", helloService());
        map.put("test","1");
        bean.setJobDataMap(map);
        return bean;
    }

    /**
     * 配置触发器 SimpleTrigger,SimpleTrigger类似于Spring Boot中的@Scheduled注解的用法
     * @return
     */
    /*@Bean
    SimpleTriggerFactoryBean simpleTriggerFactoryBean() {
        SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
        //定时任务执行的次数
        bean.setRepeatCount(5);
        //定时任务执行时间间隔
        bean.setRepeatInterval(2000);
        bean.setStartTime(new Date());
        //将JobDatail于触发器（Trigger）关联
        bean.setJobDetail(jobDetailFactoryBean().getObject());
        return bean;
    }*/



    /**
     * 配置触发器 CronTrigger，CronTrigger类似于Spring Boot中的@Scheduled注解中cron表达式的用法
     * @return
     */
    @Bean
    CronTriggerFactoryBean cronTriggerFactoryBean(){
        CronTriggerFactoryBean bean=new CronTriggerFactoryBean();
        bean.setJobDetail(jobDetailFactoryBean().getObject());
        bean.setCronExpression("0/5 * * * * ?");//每五秒一次
        return bean;
    }

    /**
     * 调度器
     * @return
     */
    @Bean
    SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setTriggers(cronTriggerFactoryBean().getObject(),cronTriggerFactoryBean().getObject());
        return bean;
    }
    @Bean
    HelloServiceImpl helloService(){
        return new HelloServiceImpl();
    }


    //另一种配置定时任务的方式
    /*@Autowired
    HelloService helloService;
    @Bean
    public JobDetail checkDevStatusDetail() {
        JobDataMap map=new JobDataMap();
        map.put("helloService",helloService);
        map.put("test","1");
        return JobBuilder.newJob(MyJob.class).withIdentity("myJob").usingJobData(map).storeDurably().build();
    }

    @Bean
    public Trigger testQuartzTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(5)  //设置时间周期单位秒,目前设置为2分钟一次
                .withRepeatCount(3)
                ;
        return TriggerBuilder.newTrigger().forJob(checkDevStatusDetail())
                .withIdentity("myJob")
                .withSchedule(scheduleBuilder)
                .build();
    }*/
}
